<?php
namespace Home\Controller;
use Think\Controller;
/**
 * Class ArticleController
 * @package Admin\Controller
 * 2017-12-13   文章类
 */
class ArticleController extends AdminBasicController{

    public $article = '';
    public $shop_id;
    public function _initialize(){
        $this->checkLogin();
        $this->article = D('Article');
        $this->shop_id = session('SHOP_ID');
    }

    /**
     * 文章列表
     * @author mss
     * @time 2017-12-13
     */
    public function articleList(){
        $where = array();
        $param = array();
        $where['status'] = 0;
        $where['shop_id'] = $this->shop_id;
        $order = 'ctime DESC';
        /**按文章标题搜索*/
        if(!empty(trim(I('request.title')))){
            $title = trim(I('request.title'));
            $where['title'] = array('LIKE','%'.$title.'%');
            $param['title'] = $title;
        }
        /**按发布时间查找*/
        if(!empty(I('request.start_time')) && !empty(I('request.end_time'))){
            $start_time = I('request.start_time');
            $this->assign("start_time",$start_time);
            $end_time = I('request.end_time');
            $this->assign("end_time",$end_time);
            $where['ctime'] = array(array('EGT',strtotime($start_time)),array('ELT',strtotime($end_time)),'and');
            $parameter['start_time'] = I('request.start_time');
            $parameter['end_time'] = I('request.end_time');
        }

        $list = $this->article->selectArticle($where,$order,15,$param);
        $data = $list['list'];


        $this->assign('list',$data);
        $this->assign('page',$list['page']);
        $this->assign('request',$_REQUEST);

        $this->display('articleList');
    }

    /**
     * 添加文章
     * @author mss
     * @time 2017-11-24
     */
    public function addArticle(){
        if(IS_POST){
            $data = $this->article->create();
            if($data){
                if (get_magic_quotes_gpc()) {
                    $data['content'] = stripslashes($_POST['content']);
                } else {
                    $data['content'] = $_POST['content'];
                }
                $data['shop_id'] = $this->shop_id;
                $res = $this->article->addArticle($data);
                if($res){
                    $this->success('添加成功',U('Article/articleList'));
                }else{
                    $this->error('添加失败');
                }
            }else{
                $this->error($this->article->getError());
            }
        }else{

            $this->display('addArticle');
        }

    }

    /**
     * 编辑文章
     * @author mss
     * @time 2017-11-24
     * @param art_id 文章id
     */
    public function editArticle(){
        if(IS_POST){
            $data = $this->article->create();
            if($data){
                $where['art_id'] = $_POST['art_id'];
                if (get_magic_quotes_gpc()) {
                    $data['content'] = stripslashes($_POST['content']);
                } else {
                    $data['content'] = $_POST['content'];
                }
                $data['utime'] = time();
                $res = $this->article->editArticle($where,$data);
                if($res){
                    $this->success('编辑成功',U('Article/articleList'));
                }else{
                    $this->error('编辑失败');
                }
            }else{
                $this->error($this->article->getError());
            }
        }else{
            $id = I('art_id');
            $info = M('Article')->where(array('art_id'=>$id))->find();
            $this->assign('info',$info);
            $this->display('editArticle');
        }
    }

    /**
     * 删除文章
     * @author mss
     * @time 2017-11-24
     */
    public function delArticle(){
        if(empty($_REQUEST['art_id'])){
            $this->error('您未选择任何操作对象');
        }
        $where['art_id'] = array('IN',I('request.art_id'));
        $data['status'] = 9;
        $data['utime'] = time();
        $upd_res = $this->article->editArticle($where,$data);
        if($upd_res){
            //其他删除操作

            $this->success('删除操作成功');
        }else{
            $this->error('删除操作失败');
        }
    }

    /**
     * 修改文章显示顺序
     */
    public function ajaxUpArticle(){
        $art_id = $_POST['art_id'];
        $data['sort'] = $_POST['sort'];
        $data['utime'] = time();
        $where['art_id'] = $art_id;
        $res = M('Article')->where($where)->save($data);
        if($res){
            $this->ajaxReturn(1);
        }else{
            $this->ajaxReturn(0);
        }
    }
    /**
     * 编辑文章显示与否
     */
    public function ajaxStatus(){
        $id = $_POST['id'];
        $status = $_POST['status'];
        $w['art_id'] = $id;
        $data['status'] = $status;
        $data['utime'] = time();
        $res = $this->article->editArticle($w,$data);
        if($res){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }

    /**ajax上传图片*/
    public function uploadPic(){
        $pic       = $_POST['pic'];
        $pic_name      = $_POST['pic_name'];
        $temp = explode('.',$pic_name);
        $ext = uniqid().'.'.end($temp);
        $base64    = substr(strstr($pic, ","), 1);
        $image_res = base64_decode($base64);
        $pic_link  = "Uploads/Article/".date('Y-m-d').'/'.$ext;
        $saveRoot = "Uploads/Article/".date('Y-m-d').'/';
        /**检查目录是否存在  循环创建目录*/
        if(!is_dir($saveRoot)){
            mkdir($saveRoot, 0777, true);
        }
        $res = file_put_contents($pic_link ,$image_res);
        if($res){
            $ajaxData = array("flag" => "success", "message"=>"上传成功！" );
            $result_data['path'] = $pic_link;
            $ajaxData['data'] = $result_data;
            $this->ajaxReturn(json_encode($ajaxData));
        }else{
            $ajaxData = array("flag" => "error", "message"=>"上传失败","data" => array());
            $this->ajaxReturn(json_encode($ajaxData));
        }
    }

    /**删除上传的相册的图片*/
    public function delPhoto(){
        $w['id'] = $_POST['id'];
        $res = M("Article")->where($w)->find();
        $pic = "";
        $type = 1;
        if($type == 1){
            $data['pic'] = "";
            $pic = M("Article")->where($w)->limit(1)->save($data);
        }elseif ($type == 2){
            $string1 = explode(",",$res['pic']);
            foreach ($string1 as $k=>$v){
                if($v == $_POST['file_path']){
                    unset($string1[$k]);
                }
            }
            if(!empty($string1)){
                $string_other = implode(',',$string1);
                $data['pic'] = $string_other;
                $pic = M("Shop")->where($w)->limit(1)->save($data);
            }else{
                $data['pic'] = "";
                $pic = M("Shop")->where($w)->limit(1)->save($data);
            }
        }elseif ($_POST['type'] == 3){
            $string2 = explode(",",$res['more_pic']);
            foreach ($string2 as $k=>$v){
                if($v == $_POST['file_path']){
                    unset($string2[$k]);
                }
            }
            if(!empty($string2)){
                $string_other = implode(',',$string2);
                $data['more_pic'] = $string_other;
                $pic = M("Shop")->where($w)->limit(1)->save($data);
            }else{
                $data['more_pic'] = "";
                $pic = M("Shop")->where($w)->limit(1)->save($data);
            }
        }
        $file  = $_POST['file_path'];
        $result = @unlink($file);
        if ($result == false && empty($pic)) {
            $this->ajaxReturn(0);
        } else {
            $this->ajaxReturn(1);
        }
    }

}